﻿package client;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.HeadlessException;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.MessageDigest;

import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JDialog;

import java.net.MalformedURLException;
import java.rmi.*;

import client.*;
import server.PersonInterface;



public class LogInGUI extends JFrame implements ActionListener{

	private JPanel contentPane;
	private JTextField textField_name;
	private JPasswordField textField_password;
	JButton button_reg;
	JButton button_log;
	private JButton button_FindPW;
	private JLabel login_label;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LogInGUI frame = new LogInGUI("虚拟城市登录");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LogInGUI(String title) {
		super(title);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField_name = new JTextField();
		textField_name.setBounds(187, 45, 105, 21);
		contentPane.add(textField_name);
		textField_name.setColumns(10);
		
		JLabel label = new JLabel("\u7528\u6237\u540D\uFF1A");
		label.setBounds(66, 42, 83, 26);
		label.setHorizontalAlignment(SwingConstants.TRAILING);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("\u5BC6  \u7801\uFF1A");
		label_1.setHorizontalAlignment(SwingConstants.TRAILING);
		label_1.setBounds(66, 102, 83, 26);
		contentPane.add(label_1);
		
		textField_password = new JPasswordField();
		textField_password.setBounds(187, 105, 105, 21);
		contentPane.add(textField_password);
		textField_password.setColumns(10);
		
		button_log = new JButton("\u767B\u5F55");
		button_log.addActionListener(this);
		button_log.setBounds(66, 167, 93, 23);
		contentPane.add(button_log);
		
		button_reg = new JButton("\u6CE8\u518C");
		button_reg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				RegGUI RegDlg=new RegGUI();
				RegDlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				RegDlg.setVisible(true);
				dispose();
			}
		});
		button_reg.setBounds(254, 167, 93, 23);
		contentPane.add(button_reg);
		button_reg.addActionListener(this);
		
		/*button_FindPW = new JButton("\u5FD8\u8BB0\u5BC6\u7801\uFF1F");
		button_FindPW.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FindPasswordGUI fpDlg=new FindPasswordGUI(null,"hello2",true);
				fpDlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				fpDlg.setVisible(true);
			}
		});
		button_FindPW.setBounds(311, 104, 93, 23);
		contentPane.add(button_FindPW);*/
		//button_FindPW.addActionListener(this);
		
		login_label=new JLabel("");
		login_label.setBounds(114, 140, 93, 23);
		contentPane.add(login_label);
	}
	
	public void actionPerformed(ActionEvent event)
	{
		if(event.getSource()==button_log)
		{
			String username=textField_name.getText().toString();
			String password=textField_password.getText().toString();

			Configuration config=new Configuration();
			PersonInterface person=null;
			
			String rmiAddress=config.getRmiAddress();
			try {
				person = (PersonInterface) Naming.lookup(rmiAddress+"Person");
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (NotBoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} 
			

			if(username.equals("") || password.equals("")){
				JOptionPane.showMessageDialog(null, "用户名或密码不能为空");
			} else{
				try {
					int pid=person.loginConfirm(username,password);
					if( pid != -1){
						Map dialog;
						try {
							dispose();
							dialog = new Map(pid);
							dialog.setLocationRelativeTo(null);
							dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
							dialog.setVisible(true);
						} catch (MalformedURLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (NotBoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
					}else{
						JOptionPane.showMessageDialog(null, "登录失败！！");
					}
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
		}
		}
		
		else if(event.getSource()==button_reg)
		{
			/*RegGUI RegDlg=new RegGUI();
			RegDlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			RegDlg.setVisible(true);*/
		}
		
	}
}